//-----------------------------------------------------------------------
// <copyright file="RegistrationSubscriptionElement.cs" company="iron9light">
//      Copyright (c) iron9light. All rights reserved.
// </copyright>
// <author>iron9light</author>
//-----------------------------------------------------------------------

namespace EventBrokerExtension.Configuration
{
    using System.Configuration;

    using Microsoft.Practices.Unity;

    /// <summary>
    /// Configuration element for configuring registration subscriptions.
    /// </summary>
    public class RegistrationSubscriptionElement : RegistrationMemberElement
    {
        /// <summary>
        /// Gets name of this element - used when calculating the collection key.
        /// </summary>
        public override string ElementName
        {
            get { return "subscription"; }
        }

        /// <summary>
        /// Gets the collection of <see cref="MethodParameterElement"/> elements
        /// that are children of this node.
        /// </summary>
        [ConfigurationProperty("", IsDefaultCollection = true)]
        [ConfigurationCollection(typeof(MethodParameterElementCollection), AddItemName = "param")]
        public MethodParameterElementCollection Parameters
        {
            get
            {
                MethodParameterElementCollection parameters = (MethodParameterElementCollection)this[string.Empty];
                parameters.TypeResolver = TypeResolver;
                return parameters;
            }
        }

        /// <summary>
        /// Return the InjectionMember object represented by this configuration
        /// element.
        /// </summary>
        /// <returns>The injection member object.</returns>
        public override InjectionMember CreateInjectionMember()
        {
            MethodParameter[] values = new MethodParameter[this.Parameters.Count];
            int i = 0;
            foreach (MethodParameterElement element in this.Parameters)
            {
                values[i++] = element.CreateInjectionParameterValue();
            }

            return new RegistrationSubscription(this.Name, this.EventTopic, values);
        }
    }
}